using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce
{
    public class Watermelon : Entity
    {
        private WatermelonData m_WatermelonData;

        public WatermelonData WatermelonData
        {
            get { return m_WatermelonData; }
            set { m_WatermelonData = value; }
        }

        private bool m_IsEnter;

        public bool IsEnter
        {
            get { return m_IsEnter; }
            set { m_IsEnter = value; }
        }

        public WatermelonType Type
        {
            get { return m_WatermelonData.WatermelonType; }
        }

        private Rigidbody2D m_Rigidbody2D;

        private Collider2D m_Collider2D;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
            m_Rigidbody2D = transform.GetComponent<Rigidbody2D>();
            m_Collider2D = transform.GetComponent<Collider2D>();
        }

        protected override void OnShow(object userData)
        {
            base.OnShow(userData);

            m_WatermelonData = (WatermelonData) userData;
            transform.localPosition = m_WatermelonData.Position1;
            m_IsEnter = false;
            if (!m_WatermelonData.Compose)
            {
                m_Rigidbody2D.bodyType = RigidbodyType2D.Static;
                m_Collider2D.enabled = false;
            }

        }

        public void Move(float x)
        {
            transform.SetLocalPositionX( x);
            m_Rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
            m_Collider2D.enabled = true;
            WatermelonManager.Instance.CreateRandomWaterMelon(0,false);
        }

        private void OnCollisionEnter2D(Collision2D other)
        {
            if (!m_IsEnter)
            {
                if (other.collider.tag == "Watermelon")
                {
                    Watermelon item = other.collider.transform.GetComponent<Watermelon>();
                    if (item.Type != WatermelonType.Red)
                    {
                        item.IsEnter = true;
                        m_IsEnter = true;
                        WatermelonManager.Instance.Check(this, item);
                    }
                }
            }

            
        }


        private void OnCollisionExit2D(Collision2D other)
        {
            m_IsEnter = false;
        }
    }
}